define(["jQuery", "Underscore", "Backbone"], function($, _, Backbone) {

	var headerTemplate = 	"<article class=\"blogPost\"><header>" +
							"<h2><%= model.get(\"title\")%></h2>" +
							"<p>Posted on <time><%= new Date(model.get(\"date\")).toDateString() %></time></p>" +
							"</article>";

	var snippetTemplate = 	"<pre class=\"brush: \"></pre>";
	var paragrafTemplate = 	"<p></p>";
	var articleTemplate = 	"<div></div>";

	var snippetRegex = new RegExp("[^\\[\\[].*(?=\\]\\])", "im");

	return Backbone.View.extend({
		tagName: "section",
		blogPostUrl: "http://springleafweb.appspot.com/get-blog-post",

		initialize: function() {
			console.log("BlogPostView - Initialize");

			this.headerTemplate = _.template(headerTemplate);
			this.$snippetTemplate = $(snippetTemplate);
			this.$paragrafTemplate = $(paragrafTemplate);
			this.$article = $(articleTemplate);

			this.model.on("change", this.render, this);
		},

		render: function() {
			console.log("BlogPostView - Render");

			if (this.model.get("name") != this.options.name) {
				this.requestBlogPost(this.options.name);
			} else {
				this.renderBlogPost();
			}

			return this;
		},

		renderBlogPost: function() {
			this.$el.empty().append(this.headerTemplate({
				model: this.model
			})).append(this.$article.empty());

			this.renderArticle();
		},

		renderArticle: function() {
			var paragrafs = this.model.get("content").split("\n\n\n");
			var value;

			for (var i = 0; i < paragrafs.length; i++) {
				value = snippetRegex.exec(paragrafs[i]);

				if (value == null) {
					this.renderParagraf(paragrafs[i]);
				} else {
					this.renderSnippet(value, paragrafs[i]);
				}
			}
		},

		renderParagraf: function(data) {
			this.$article.append(this.$paragrafTemplate.clone().text(data));
		},

		renderSnippet: function(value, data) {
			value = value.toString();
			var length = value.length;

			this.$article.append(this.$snippetTemplate.clone().
				addClass(value).text(data.slice(length + 4, -length - 5)));

			require(["libs/" + value], function () {
				SyntaxHighlighter.highlight();
			});
		},

		requestBlogPost: function(name) {
			console.log("BlogView - Request ~ BlogPost");
			this.model.fetch();

			var self = this;
			var url = this.blogPostUrl + (name != null ? "?post=" + name : "");

			$.get(url, function(event) {
				if (event.error == null && event != "") {
					self.model.set({
						title: event.data.title,
						date: event.data.date,
						content: event.data.content,
						name: name
					});
				} else {
					alert("Error getting the post");
				}
			}).error(function() {
				event = {
					action: "GET_BLOG_POST",
					data: {
						title: "MemCache or how to share with GAE",
						content: "Well, one of the features that GAE provides is the MemCache and this could do the trick.\n\n\nMemCache, or Memory Cache, is a shared memory.\n\n\n[[JAVA]]\npublic static BattleEngine getGame(String id) {\n\tMap<String, BattleEngine> map = container.getAll(setCache.getValues());\n\tSet<Map.Entry<String, BattleEngine>> entries = map.entrySet();\n\t\n\tIterator<Map.Entry<String, BattleEngine>> iterator = entries.iterator();\n\twhile (iterator.hasNext()) {\n\t\tBattleEngine game = iterator.next().getValue();\n\t\tif (game.getId().equals(id)) {\n\t\t\treturn game;\n\t\t}\n\t}\n\treturn null;\n}\n\npublic static void deleteGame(Long userId) {\n\tMap<String, BattleEngine> map = container.getAll(setCache.getValues());\n\tSet<Map.Entry<String, BattleEngine>> entries = map.entrySet();\n\t\n\tIterator<Map.Entry<String, BattleEngine>> iterator = entries.iterator();\n\twhile (iterator.hasNext()) {\n\t\tBattleEngine game = iterator.next().getValue();\n\t\tif (game.containsId(userId)) {\n\t\t\tcontainer.remove(game);\n\t\t\tsetCache.remove(game.getId());\n\t\t}\n\t}\n}\n\n[[/JAVA]\n\n\nWell, one of the features that GAE provides is the MemCache and this could do the trick.\n\n\n[[JAVA]]\npublic static BattleEngine getGame(String id) {\n\tMap<String, BattleEngine> map = container.getAll(setCache.getValues());\n\tSet<Map.Entry<String, BattleEngine>> entries = map.entrySet();\n\t\n\tIterator<Map.Entry<String, BattleEngine>> iterator = entries.iterator();\n\twhile (iterator.hasNext()) {\n\t\tBattleEngine game = iterator.next().getValue();\n\t\tif (game.getId().equals(id)) {\n\t\t\treturn game;\n\t\t}\n\t}\n\treturn null;\n}\n\npublic static void deleteGame(Long userId) {\n\tMap<String, BattleEngine> map = container.getAll(setCache.getValues());\n\tSet<Map.Entry<String, BattleEngine>> entries = map.entrySet();\n\t\n\tIterator<Map.Entry<String, BattleEngine>> iterator = entries.iterator();\n\twhile (iterator.hasNext()) {\n\t\tBattleEngine game = iterator.next().getValue();\n\t\tif (game.containsId(userId)) {\n\t\t\tcontainer.remove(game);\n\t\t\tsetCache.remove(game.getId());\n\t\t}\n\t}\n}\n[[/JAVA]",
						date: "12-3-2013 22:32"
					}
				};
				self.model.set({
					title: event.data.title,
					date: event.data.date,
					content: event.data.content,
					name: name
				});
			});
		}
	});
});